package com.maven.zh.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

public class FileUploadUtil {

	public static String uploadpic(HttpServletRequest req,String path) throws IOException{
		InputStream in = null;
		FileOutputStream out = null;
		String filename = null;
		try{
			MultipartHttpServletRequest request = (MultipartHttpServletRequest)req;
		
			Iterator<String> iterator = request.getFileNames();
			while(iterator.hasNext()){
				filename = iterator.next();
				MultipartFile file = request.getFile(filename);
				filename = file.getOriginalFilename();
				in = file.getInputStream();
				out = new FileOutputStream(new File(path+"/"+filename));
				
				int i = -1;
				while((i = in.read()) != -1){
					out.write(i);
				}
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			in.close();
			out.close();
		}
		return filename;
	}

}
